import axios from 'axios'
import { getToken } from '@/utils/auth'
import {  Message } from 'element-ui'
import cache from '@/plugins/cache' 
import { tansParams } from "@/utils/ruoyi";

console.log('process.env.VUE_APP_BASE_API',process.env.VUE_APP_BASE_API)
// 创建一个axios实例
const service = axios.create({
    baseURL:process.env.VUE_APP_BASE_API, // url = base url + request url
    withCredentials: true, // send cookies when cross-domain requests
    timeout: 10000 // request timeout 10s
});

// 请求拦截器
service.interceptors.request.use(config => {
    const isToken = (config.headers || {}).isToken === false
    // console.log('isToken',isToken)
    var token = getToken() 
    // console.log('token555',token)
    if (token && !isToken) {
        config.headers['X-Access-Token'] = getToken()
    }
    if (config.method === 'get' ) {
      if(config.params){
        let url = config.url + '?' + tansParams(config.params);
        url = url.slice(0, -1);
        config.params = {};
        config.url = url;
      }else{
       
        let url = config.url
        console.log('url',config.url)
        config.url = url;
      }
        
    }
    if (config.method === 'post' || config.method === 'put') {
        const requestObj = {
          url: config.url,
          data: typeof config.data === 'object' ? JSON.stringify(config.data) : config.data,
          time: new Date().getTime()
        }
        const sessionObj = cache.session.getJSON('sessionObj')
        if (sessionObj === undefined || sessionObj === null || sessionObj === '') {
          cache.session.setJSON('sessionObj', requestObj)
        } else {
          const s_url = sessionObj.url;                  // 请求地址
          const s_data = sessionObj.data;                // 请求数据
          const s_time = sessionObj.time;                // 请求时间
          const interval = 1000;                         // 间隔时间(ms)，小于此时间视为重复提交
          if (s_data === requestObj.data && requestObj.time - s_time < interval && s_url === requestObj.url) {
            const message = '数据正在处理，请勿重复提交';
            console.warn(`[${s_url}]: ` + message)
            return Promise.reject(new Error(message))
          } else {
            cache.session.setJSON('sessionObj', requestObj)
          }
        }
      }
    // do something before request is sent
    return config;
},error => {
    // do something with request error
    console.log(error) // for debug
    return Promise.reject(error);
});

// 响应拦截器
service.interceptors.response.use(response => {
    //do something before response
    const res = response.data
 
    if (res.code !== 200) {
        console.log('res.message',res.message)
        Message({
          message: res.message || 'Error',
          type: 'error',
          duration: 5 * 1000
        })
        return Promise.reject(new Error(res.message || 'Error'))
    } else {
      return res
    }
},
error => {
    console.log('err', error); // for debug
    Message({
        message: error.message,
        type: 'error',
        duration: 5 * 1000
    })
    return Promise.reject(error);
})

export default service;